﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using HarmonyLib;
using KMod;
using TUNING;
using UnityEngine;

namespace ONI_ModRock
{

    public class MySteamRocket : UserMod2
    {
        public override void OnLoad(Harmony harmony)
        {
            Debug.Log("MySteamRocket I execute OnLoad! ");
            base.OnLoad(harmony);
            float newValue = 600f;
            //Traverse.Create(typeof(BUILDINGS.ROCKETRY_MASS_KG)).Field<float[]>("FUEL_TANK_WET_MASS_GAS_LARGE").Value[0] = newValue;
            //Traverse.Create(typeof(ROCKETRY.ENGINE_POWER)).Field<int>("MID_WEAK").Value *= 3;

            Traverse.Create(typeof(BUILDINGS.ROCKETRY_MASS_KG)).Field<float[]>("FUEL_TANK_DRY_MASS").Value[0] *= 3;
            Traverse.Create(typeof(BUILDINGS.ROCKETRY_MASS_KG)).Field<float[]>("FUEL_TANK_WET_MASS").Value[0] *= 3;
            Traverse.Create(typeof(BUILDINGS.ROCKETRY_MASS_KG)).Field<float[]>("FUEL_TANK_WET_MASS_SMALL").Value[0] *= 3;
            Traverse.Create(typeof(BUILDINGS.ROCKETRY_MASS_KG)).Field<float[]>("FUEL_TANK_WET_MASS_GAS").Value[0] *= 3;
            Traverse.Create(typeof(BUILDINGS.ROCKETRY_MASS_KG)).Field<float[]>("FUEL_TANK_WET_MASS_GAS_LARGE").Value[0] *= 3;
            Traverse.Create(typeof(BUILDINGS.ROCKETRY_MASS_KG)).Field<float[]>("OXIDIZER_TANK_OXIDIZER_MASS").Value[0] *= 3;




            {
                System.Type enginePowerType = typeof(ROCKETRY.ENGINE_POWER);
                FieldInfo[] fields = enginePowerType.GetFields(BindingFlags.Public | BindingFlags.Static);
                foreach (FieldInfo field in fields)
                {
                    if (field.FieldType == typeof(int))
                    {
                        int originalValue = (int)field.GetValue(null);
                        int newValue1 = originalValue * 3;
                        Traverse.Create(enginePowerType).Field(field.Name).SetValue(newValue1);
                        Debug.Log($"Modified {field.Name} from {originalValue} to {newValue1}");
                    }
                }
            }
            {
                System.Type enginePowerType = typeof(ROCKETRY.FUEL_COST_PER_DISTANCE);
                FieldInfo[] fields = enginePowerType.GetFields(BindingFlags.Public | BindingFlags.Static);
                foreach (FieldInfo field in fields)
                {
                    if (field.FieldType == typeof(float))
                    {
                        float originalValue = (float)field.GetValue(null);
                        float newValue1 = originalValue / 2.0f;
                        Traverse.Create(enginePowerType).Field(field.Name).SetValue(newValue1);
                        Debug.Log($"Modified {field.Name} from {originalValue} to {newValue1}");
                    }
                }
            }
            {
                System.Type scoutbotType = typeof(ROBOTS.SCOUTBOT);

                // 修改 DIGGING 字段
                FieldInfo diggingField = scoutbotType.GetField("DIGGING", BindingFlags.Public | BindingFlags.Static);
                if (diggingField != null)
                {
                    diggingField.SetValue(null, 100f); // 将 DIGGING 设置为 2f
                    Debug.Log($"ROBOTS.SCOUTBOT.DIGGING modified to: {2f}");
                }

                // 修改 CONSTRUCTION 字段
                FieldInfo constructionField = scoutbotType.GetField("CONSTRUCTION", BindingFlags.Public | BindingFlags.Static);
                if (constructionField != null)
                {
                    constructionField.SetValue(null, 100f); // 将 CONSTRUCTION 设置为 3f
                    Debug.Log($"ROBOTS.SCOUTBOT.CONSTRUCTION modified to: {3f}");
                }

                // 修改 ATHLETICS 字段
                FieldInfo athleticsField = scoutbotType.GetField("ATHLETICS", BindingFlags.Public | BindingFlags.Static);
                if (athleticsField != null)
                {
                    athleticsField.SetValue(null, 100f); // 将 ATHLETICS 设置为 4f
                    Debug.Log($"ROBOTS.SCOUTBOT.ATHLETICS modified to: {4f}");
                }

                // 修改 BATTERY_CAPACITY 字段
                FieldInfo batteryCapacityField = scoutbotType.GetField("BATTERY_CAPACITY", BindingFlags.Public | BindingFlags.Static);
                if (batteryCapacityField != null)
                {
                    batteryCapacityField.SetValue(null, 30f * 30f * 600f); // 将 BATTERY_CAPACITY 设置为 60f * 10f * 600f
                    Debug.Log($"ROBOTS.SCOUTBOT.BATTERY_CAPACITY modified to: {30f * 100f * 600f}");
                }
            }
            Debug.Log("MySteamRocket I execute OnLoad END!");
        }

        [HarmonyPatch(typeof(RocketInteriorLiquidInputPortConfig))]
        [HarmonyPatch("ConfigureBuildingTemplate")]
        public class InputPortConfigPatch
        {
            public static void Postfix(GameObject go)
            {
                Debug.Log("RocketInteriorLiquidInputConfig !");
                RocketConduitSender rocketConduitSender = go.GetComponent<RocketConduitSender>();
                rocketConduitSender.conduitStorage.capacityKg = 100f;
                Debug.Log("RocketInteriorLiquidInputConfig end!");
            }
        }

        [HarmonyPatch(typeof(RocketConduitReceiver.ConduitPort))]
        [HarmonyPatch("SetPortInfo")]
        public class OutputPortConfigPatch
        {
            [HarmonyPrefix]
            public static void Prefix(Storage senderStorage)
            {
                Debug.Log("RocketInteriorLiquidInputConfig !");
                if (senderStorage != null)
                {
                    senderStorage.capacityKg = 100f; 
                    Debug.Log($"RocketConduitReceiver capacityKg modified to: {senderStorage.capacityKg}");
                }
                Debug.Log("RocketInteriorLiquidInputConfig end!");
            }
        }

    }
}
